using micromsgBackground;
using MicroMsgBackground.Common.Utils;
using MicroMsgBackground.Network;
using MicroMsgBackground.Storage;
using Microsoft.Phone.Info;
using System;
using System.Collections.Generic;
using System.IO.IsolatedStorage;
namespace MicroMsgBackground.Manager
{
	public class AccountMgr
	{
		public const string TAG = "AccountMgr";
		public const string bgDir = "background";
		public const string bgAccFilePath = "background/accountinfo.xml";
		public const string bgSyncdataFilePath = "background/syncdata.xml";
		public const string bgFlowdataFilePath = "background/flowdata.xml";
		private static AccountBackground curAccount = new AccountBackground();
		public static string mConstAccountInfo = "";
		public static string mConstDeviceInfo = string.Concat(new object[]
		{
			"DeviceID = ",
			Util.getDeviceUniqueId(),
			", Name=",
			DeviceStatus.get_DeviceName(),
			", Manufacturer=",
			DeviceStatus.get_DeviceManufacturer(),
			", FW=",
			DeviceStatus.get_DeviceFirmwareVersion(),
			", Power=",
			DeviceStatus.get_PowerSource(),
			", OS =",
			Environment.get_OSVersion().get_Platform(),
			", OSVer=",
			Environment.get_OSVersion().get_Version(),
			", HW=",
			DeviceStatus.get_DeviceHardwareVersion()
		});
		public static bool isLogin
		{
			get;
			private set;
		}
		public static string curUserName
		{
			get
			{
				if (AccountMgr.curAccount.strUsrName == null)
				{
					return "";
				}
				return AccountMgr.curAccount.strUsrName;
			}
		}
		public static AccountBackground getCurAccount()
		{
			return AccountMgr.curAccount;
		}
		public static bool hasValidAccount()
		{
			return AccountMgr.curAccount != null && (AccountMgr.curAccount.strUsrName != null && AccountMgr.curAccount.strUsrName.get_Length() > 0) && (AccountMgr.curAccount.strPwd != null && AccountMgr.curAccount.strPwd.get_Length() > 0);
		}
		public static bool loadAccount()
		{
			AccountBackground accountBackground = StorageXml.loadFromFile<AccountBackground>("background/accountinfo.xml");
			if (accountBackground == null)
			{
				AccountMgr.curAccount = new AccountBackground();
				Log.smethod_5("AccountMgr", "load account failed");
				return false;
			}
			Log.smethod_5("AccountMgr", "load account success");
			AccountMgr.curAccount = accountBackground;
			return true;
		}
		private static bool isSessionkeyValid(byte[] Sessionkey)
		{
			if (Sessionkey != null && Sessionkey.Length > 0)
			{
				for (int i = 0; i < Sessionkey.Length; i++)
				{
					if (Sessionkey[i] != 0)
					{
						return true;
					}
				}
				return false;
			}
			return false;
		}
		public static string GetCurrentLanguage()
		{
			return AccountMgr.curAccount.currentLanguage;
		}
		public static void saveBackgroundAccout(AccountBackground accountBG)
		{
			using (IsolatedStorageFile userStoreForApplication = IsolatedStorageFile.GetUserStoreForApplication())
			{
				if (!userStoreForApplication.DirectoryExists("background"))
				{
					userStoreForApplication.CreateDirectory("background");
				}
			}
			StorageXml.saveToFile<AccountBackground>(accountBG, "background/accountinfo.xml");
		}
		public static void reSetBackgroundAccout()
		{
			AccountMgr.curAccount.strPwd = null;
			AccountMgr.curAccount.strPwd2 = null;
			AccountMgr.curAccount.nUin = 0u;
			AccountMgr.curAccount.bytesCurSyncKey = null;
			AccountMgr.curAccount.bytesMaxSyncKey = null;
			AccountMgr.curAccount.bytesSessionkey = null;
			AccountMgr.curAccount.bytesSyncKeyBuf = null;
			StorageXml.saveToFile<AccountBackground>(AccountMgr.curAccount, "background/accountinfo.xml");
		}
		public static void saveSyncData(NewSyncRequest request, NewSyncResponse response, byte[] syncKey)
		{
			Log.smethod_6("AccountMgr", "newsync doscene saveSyncData in");
			List<SyncData> list = AccountMgr.loadSyncData();
			if (list == null)
			{
				list = new List<SyncData>();
			}
			list.Add(new SyncData
			{
				requestBuf = request.ToByteArray(),
				responseBuf = response.ToByteArray(),
				syncKey = syncKey
			});
			StorageXml.saveToFile<List<SyncData>>(list, "background/syncdata.xml");
			Log.smethod_6("AccountMgr", "newsync doscene saveSyncData out");
		}
		public static List<SyncData> loadSyncData()
		{
			Log.smethod_6("AccountMgr", "newsync doscene loadSyncData in");
			List<SyncData> result = StorageXml.loadFromFile<List<SyncData>>("background/syncdata.xml");
			Log.smethod_6("AccountMgr", "newsync doscene loadSyncData out");
			return result;
		}
		public static void saveFlowData(uint uint_0, uint isSend)
		{
			Log.smethod_6("AccountMgr", "newsync doscene saveFlowData in");
			List<FlowData> list = AccountMgr.loadFlowData();
			if (list == null)
			{
				list = new List<FlowData>();
			}
			list.Add(new FlowData
			{
				uint_0 = uint_0,
				networkType = NetworkDeviceWatcher.isCurrentWifi() ? 1u : 0u,
				isSend = isSend,
				time = (ulong)Util.getNowSeconds()
			});
			StorageXml.saveToFile<List<FlowData>>(list, "background/flowdata.xml");
			Log.smethod_6("AccountMgr", "newsync doscene saveFlowData out");
		}
		public static List<FlowData> loadFlowData()
		{
			Log.smethod_6("AccountMgr", "newsync doscene loadFlowData in");
			List<FlowData> result = StorageXml.loadFromFile<List<FlowData>>("background/flowdata.xml");
			Log.smethod_6("AccountMgr", "newsync doscene loadFlowData out");
			return result;
		}
		public static void delBackgroundData()
		{
			Log.smethod_6("AccountMgr", "newsync doscene delBackgroundData in");
			StorageXml.deleteFile<AccountBackground>("background/accountinfo.xml");
			StorageXml.deleteFile<List<SyncData>>("background/syncdata.xml");
			StorageXml.deleteFile<List<FlowData>>("background/flowdata.xml");
			Log.smethod_6("AccountMgr", "newsync doscene delBackgroundData out");
		}
	}
}
